package com.pengpeng.multdata.plus.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.pengpeng.multdata.plus.entity.User;
import com.pengpeng.multdata.plus.entity.User2;
import com.pengpeng.multdata.plus.mapper.UserMapper1;
import com.pengpeng.multdata.plus.mapper.UserMapper2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 彭鹏
 * @date 2021/10/29.
 */
@Service
@Slf4j
@EnableAspectJAutoProxy(proxyTargetClass = true,exposeProxy = true)
public class UserService {
    @Autowired
    private UserMapper1 userMapper1;
    @Autowired
    private UserMapper2 userMapper2;

    @Transactional
    public List<User> queryAll1() {
        return userMapper1.selectList(null);
    }

    @Transactional
    public List<User2> queryAll2() {
        return userMapper2.selectList(null);
    }

    private UserService self() {
        return (UserService) AopContext.currentProxy();
    }

    public void method1() {
        log.info("---------------------------{}", userMapper1.selectById(2));
        log.info("---------------------------{}", userMapper2.selectById(2));
    }
    /**
     * 报错都找db1
     */
    @Transactional
    public void method2() {
        log.info("---------------------------2{}", userMapper1.selectById(2));
        log.info("---------------------------2{}", userMapper2.selectById(2));
    }

    /**
     * 报错都找db1
     */
    public void method3(){
        self().method31();
        self().method32();
    }
    @Transactional
    public void method31(){
        log.info("---------------------------2{}", userMapper1.selectById(2));
    }
    @Transactional
    public void method32(){
        log.info("---------------------------2{}", userMapper2.selectById(2));
    }

    public void method4(){
        self().method41();
        self().method42();
    }

    @Transactional
    @DS("db1")
    public void method41(){
        log.info("---------------------------2{}", userMapper1.selectById(2));
    }
    @Transactional
    @DS("db2")
    public void method42(){
        log.info("---------------------------2{}", userMapper2.selectById(2));
    }

    @Transactional
    @DS("db1")
    public void method5(){
        log.info("---------------------------2{}", userMapper1.selectById(2));
        self().method52();
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @DS("db2")
    public void method52(){
        log.info("---------------------------2{}", userMapper2.selectById(2));
    }

    /**
     * 报错都找db1
     */
    public void method6(){
        self().method61();
        self().method62();
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void method61(){
        log.info("---------------------------2{}", userMapper1.selectById(2));
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void method62(){
        log.info("---------------------------2{}", userMapper2.selectById(2));
    }
}
